package com.itheima.advantagesup.service.impl;

import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.mapper.MenuMapper;
import com.itheima.advantagesup.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 添加
     *
     * @param sysMenu 菜单签证官
     */
    @Override
    public void add(SysMenu sysMenu) {
        sysMenu.setDefaultData("1");
        sysMenu.setIsShow(1);
        sysMenu.setIsCache(0);
        sysMenu.setIsLink(1);
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setCreateDate(LocalDateTime.now());
        menuMapper.add(sysMenu);
    }

    @Override
    public void delMenu(Integer id) {
     menuMapper.delMenu(id);
    }

    @Override
    public List<SysMenu> queryAllData() {

        List<SysMenu> list = menuMapper.selectListByPid(0);
        list.forEach(item ->{
            List<SysMenu> list1 = menuMapper.selectListByPid(item.getPid());
                list1.forEach(item1  ->{
                    List<SysMenu>list2 =menuMapper.selectListByPid(item1.getPid());
                        item1.setChildren(list2);
                });
                item.setChildren(list1);

        });
        return list;
    }



    @Override
    public void updateMenu(SysMenu sysMenu) {
        SysMenu menu = new SysMenu();
        BeanUtils.copyProperties(sysMenu , menu);

        sysMenu.setUpdateDate(LocalDateTime.now());
        menuMapper.updateMenu(menu);


    }


    @Override
    public SysMenu querySysMenuById(Integer id) {
        SysMenu sysMenu = menuMapper.seleteSysMenuById(id);
        return sysMenu;
    }




}
